package com.xl.exp.exception;

import ch.qos.logback.core.net.SyslogOutputStream;
import org.apache.shiro.authz.UnauthorizedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import sun.applet.Main;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@ControllerAdvice
@ResponseBody
public class WebExceptionHandle {

    private static Logger logger = LoggerFactory.getLogger(WebExceptionHandle.class);

    /**
     * 400 - Bad Request
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public String handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
        logger.error("参数解析失败", e);
        return e.getMessage();
    }

    /**
     * 405 - Method Not Allowed
     */
    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public String handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
        logger.error("不支持当前请求方法", e);
        return e.getMessage();
    }

    /**
     * 415 - Unsupported Media Type
     */
    @ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
    @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
    public String handleHttpMediaTypeNotSupportedException(Exception e) {
        logger.error("不支持当前媒体类型", e);
        return e.getMessage();
    }

    @ResponseStatus(HttpStatus.FORBIDDEN)
    @ExceptionHandler(UnauthorizedException.class)
    public String handleUnauthorizedException(Exception e) {
        if (e instanceof UnauthorizedException) {
            logger.debug(e.getMessage());
            return "403 无权限访问";
        } else {
            return e.getMessage();
        }
    }

    /**
     * 500 - Internal Server Error
     */
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(Exception.class)
    public String handleException(Exception e) {

        logger.error("服务运行异常", e);
        e.printStackTrace();
        return e.getMessage();
    }


    public static void main(String args[]) {
        //原始字符串
        String expression = "no process fount [system]";

        String regStr = "[^\\[]*\\[(.*)\\][^\\]]*";//"(\\[)?(.*)(\\])";
        Pattern pattern = Pattern.compile(regStr);
        Matcher matcher = pattern.matcher(expression);
        System.out.println("开始匹配");
        if(matcher.find()){
            System.out.println("匹配到" + matcher.group(1));
        }

    }


}
